package com.lintf.bot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lintf.bot.domain.BotBillCell;
import com.lintf.bot.domain.dto.BotBillCellUpdateDTO;
import com.lintf.bot.domain.dto.BotBillUpdateDTO;
import com.lintf.bot.domain.enums.StatusEnums;
import com.lintf.bot.mapper.BotBillCellMapper;
import com.lintf.bot.service.IBotBillCellService;
import com.lintf.bot.service.IBotBillService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lintf
 * @description 针对表【bot_bill_cell】的数据库操作Service实现
 * @createDate 2024-01-08 13:03:53
 */
@Service
public class BotBillCellServiceImpl extends ServiceImpl<BotBillCellMapper, BotBillCell>
        implements IBotBillCellService {

    @Autowired
    private IBotBillService botBillService;

    @Override
    public void update(BotBillCellUpdateDTO dto) {
        BotBillCell billCell = new BotBillCell();
        BeanUtils.copyProperties(dto, billCell);
//        billCell.setUpdateParams();
        baseMapper.updateById(billCell);
        LambdaQueryWrapper<BotBillCell> wrapper = new LambdaQueryWrapper<>();
        BotBillUpdateDTO updateDTO = new BotBillUpdateDTO();
        Integer botBillId = dto.getBotBillId();
        if (dto.getBotBillId() == null) {
            botBillId = baseMapper.selectById(dto.getId()).getBotBillId();
        }
        wrapper.eq(BotBillCell::getBotBillId, botBillId);
        updateDTO.setId(botBillId);
        if (baseMapper.selectList(wrapper).stream().noneMatch(botBillCell -> botBillCell.getStatus() == StatusEnums.INCOMPLETE.getValue())) {
            updateDTO.setStatus(StatusEnums.COMPLETED.getValue());
        } else {
            updateDTO.setStatus(StatusEnums.INCOMPLETE.getValue());
        }
        botBillService.update(updateDTO);
    }
}




